cmake_minimum_required(VERSION 3.10)
project(vk_hist)

set(CMAKE_CXX_STANDARD 17)

# Find Vulkan
find_package(Vulkan REQUIRED)

# Find DCMTK
find_package(DCMTK REQUIRED)
# 使用DCMTK的CMake目标而不是库名
# 这样可以确保RPATH被正确设置
set(DCMTK_LIBRARIES "DCMTK::DCMTK")
message(STATUS "Using DCMTK CMake target: ${DCMTK_LIBRARIES}")
# Find GLFW (for window creation and Vulkan surface)
find_package(glfw3 REQUIRED)

# Add executable
add_executable(vk_hist main.cpp)

# Compile shader
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader.comp.spv
    COMMAND glslc ${CMAKE_CURRENT_SOURCE_DIR}/shader.comp -o ${CMAKE_CURRENT_BINARY_DIR}/shader.comp.spv
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/shader.comp
    COMMENT "Compiling shader"
)

# Compile graphics shaders (fullscreen triangle)
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader.vert.spv
    COMMAND glslc ${CMAKE_CURRENT_SOURCE_DIR}/shader.vert -o ${CMAKE_CURRENT_BINARY_DIR}/shader.vert.spv
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/shader.vert
    COMMENT "Compiling vertex shader"
)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader.frag.spv
    COMMAND glslc ${CMAKE_CURRENT_SOURCE_DIR}/shader.frag -o ${CMAKE_CURRENT_BINARY_DIR}/shader.frag.spv
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/shader.frag
    COMMENT "Compiling fragment shader"
)

# Add shader as source
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/shader.comp.spv PROPERTIES GENERATED TRUE)
target_sources(vk_hist PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/shader.comp.spv)

set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/shader.vert.spv PROPERTIES GENERATED TRUE)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/shader.frag.spv PROPERTIES GENERATED TRUE)
target_sources(vk_hist PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/shader.vert.spv ${CMAKE_CURRENT_BINARY_DIR}/shader.frag.spv)


# Link libraries
# Use static libraries for DCMTK
target_link_libraries(vk_hist ${Vulkan_LIBRARIES} 
    ${DCMTK_LIBRARIES}
    -lz
)
target_include_directories(vk_hist PRIVATE ${Vulkan_INCLUDE_DIRS} ${DCMTK_INCLUDE_DIRS})

# Link GLFW
target_link_libraries(vk_hist glfw)

# Make sure to link all required DCMTK components
find_package(Threads REQUIRED)
target_link_libraries(vk_hist ${CMAKE_THREAD_LIBS_INIT})

# Copy shader to build directory after compilation
add_custom_command(TARGET vk_hist POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_BINARY_DIR}/shader.comp.spv
    $<TARGET_FILE_DIR:vk_hist>)

add_custom_command(TARGET vk_hist POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_BINARY_DIR}/shader.vert.spv
    $<TARGET_FILE_DIR:vk_hist>)

add_custom_command(TARGET vk_hist POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_BINARY_DIR}/shader.frag.spv
    $<TARGET_FILE_DIR:vk_hist>)
